package util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Tools {
    public static final int INDEX_NOT_FOUND = -1;
    public static Map<String, Integer> userName = new LinkedHashMap<String, Integer>();
    // 初始化角色名字和出现的频率，Map中Key为角色名字，Value为该人物名字在剧中出现的频率
    static {
        userName.put("宝玉", 0);
        userName.put("黛玉", 0);
        userName.put("宝钗", 0);
        userName.put("凤姐", 0);
        userName.put("元春", 0);
        userName.put("迎春", 0);
        userName.put("探春", 0);
        userName.put("惜春", 0);
        userName.put("史湘云", 0);
        userName.put("秦可卿", 0);
    }

    /**
     * 根据文件路径读取文件
     * 
     * @param filename
     * @return
     * @throws Exception
     */
    public static List<String> readFile(final String filename) throws Exception {
        List<String> filecon = new ArrayList<String>();
        String m = "";
        BufferedReader file = null;
        try {
            file = new BufferedReader(new FileReader(filename));
            while ((m = file.readLine()) != null) {
                if (!m.equals("0")) {
                    if (!m.equals("")) {
                        filecon.add(m);
                    }
                }
            }
        } catch (IOException e) {
            throw new Exception(e.getMessage());
        } finally {
            file.close();
        }
        return filecon;
    }

    /**
     * 将string类型数组换换位int类型数组
     * 
     * @param array
     * @return
     */
    public static int[] convertStrToInt(String[] array) {
        int[] intNum = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            intNum[i] = Integer.parseInt(array[i]);
        }
        return intNum;
    }

    /**
     * 检测指定字符在指定文本内容中出现的次数
     * 
     * @param str
     * @param chr
     * @return
     */
    public static int checkSum(String str, char chr) {
        int count = 0;
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chr == chars[i]) {
                count++;
            }
        }
        return count;
    }

    /**
     * 统计查找字符串在给定字符串中出现的次数
     * 
     * @param str
     * @param sub
     * @return
     */
    public static int countMatches(String str, String sub) {
        if (isEmpty(str) || isEmpty(sub)) {
            return 0;
        }
        int count = 0;
        int idx = 0;
        while ((idx = str.indexOf(sub, idx)) != INDEX_NOT_FOUND) {
            count++;
            idx += sub.length();
        }
        return count;
    }

    /**
     * 检测字符串是否为空
     * 
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    /**
     * 获取1千个随机整数
     * 
     * @return
     */
    public static List<Integer> getThousandNums() {
        return getRandomNums(1000);
    }

    /**
     * 获取一万个随机整数
     * 
     * @return
     */
    public static List<Integer> getTenThousandNums() {
        return getRandomNums(10000);
    }

    /**
     * 获取十万个随机整数
     * 
     * @return
     */
    public static List<Integer> getHundredThousandNums() {
        return getRandomNums(100000);
    }

    /**
     * 获取一百万个随机整数
     * 
     * @return
     */
    public static List<Integer> getMillionNums() {
        return getRandomNums(1000000);
    }

    /**
     * 获取指定数量的随机整数
     * 
     * @param size
     * @return
     */
    public static List<Integer> getRandomNums(Integer size) {
        List<Integer> num = new ArrayList<Integer>(1);
        Random random = new Random();
        for (int i = 0; i < size; i++) {
            num.add(random.nextInt());
        }
        return num;
    }
}
